package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_resource;
import cn.com.api.service.ApiU_resourceService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_resource/")
public class ApiU_resourceController {
    @Autowired
    private ApiU_resourceService apiU_resourceService;
    
	@Autowired
	private CheckService checkService;
	
	private static Logger logger = Logger.getLogger(ApiU_resourceController.class);
	
	

     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_resources")
	public void findU_resources(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_resource> u_resources = apiU_resourceService.findU_resources();
		
			// return
            map.put("datas", u_resources);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    /**
     * 获取资源接口
     * @param request
     * @param response
     * @param user_id
     * @param token
     * @throws Exception
     */
	@RequestMapping("/findU_resources_type")
	public void findU_resources_type(HttpServletRequest request, HttpServletResponse response, String user_id, String token) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> param = new HashMap<String,Object>();
		List<Map<String, Object>> u_resources_map = new ArrayList<Map<String, Object>>();
		try{
			
			if(checkService.checkToken(user_id, token, map)){
				String type = request.getParameter("type");							
				param.put("type", type);
			    List<ApiU_resource> u_resources = apiU_resourceService.findU_resources_type(param);
			    for (ApiU_resource apiU_resource : u_resources) {
			    	param = new HashMap<String,Object>();
			    	param.put("name", apiU_resource.getRes_name());
			    	param.put("url", apiU_resource.getRes_image());
			    	param.put("id", apiU_resource.getResource_id());
			    	u_resources_map.add(param);
				}
			    map.put("datas", u_resources_map);
			    map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			    map.put(Utils.MESSAGE, "查询成功！");
			}
			// return
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    

	
 
}


